package com.hornsun.client.homepage.api;

import com.hornsun.configInfo.ConfigInfo;
import com.hornsun.domain.constant.types.InfoType;
import com.hornsun.domain.dbo.Commodity;
import com.hornsun.domain.dbo.ShowInfor;
import com.hornsun.domain.dbo.Information;
import com.hornsun.client.homepage.dto.HomepageDTO;
import com.hornsun.repository.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by 编译中... on 2018/4/4.
 */
@RestController
@RequestMapping("/api/homepage")
public class HomepageAPI {

    @Resource
    private HealthIndexRepository healthIndexRepository;

    @Resource
    private InformationRepository informationRepository;

    @Resource
    private CommodityRepository commodityRepository;

    @Resource
    private SlideshowRepository slideshowRepository;

    @Resource
    private ConfigInfo configInfo;

    @Resource
    private MemberWechatRepository memberWechatRepository;

    @GetMapping("/toHomepage")
    public ResponseEntity<HomepageDTO> toHomepage(HttpServletRequest httpRequest){
        String openid = httpRequest.getAttribute("openid").toString();
        String memberGuid = memberWechatRepository.getMemberGuidByOpenid(openid);
        int page = 1;
        List<ShowInfor> homeRecommendList=  getHomeRecommend();
        List<ShowInfor> list = new ArrayList<>();
        for(int i=0;i<homeRecommendList.size();i++){
            if(i>=(page-1)* configInfo.getLimit()&&i<page*configInfo.getLimit()){
                list.add(homeRecommendList.get(i));
            }
        }
        HomepageDTO homepageDTO = new HomepageDTO();
        homepageDTO.setHomeRecommends(list);
        homepageDTO.setSlideshows(slideshowRepository.selectSlideshow());
        homepageDTO.setHealthIndex(healthIndexRepository.getTodayHealthIndex(memberGuid));
        return new ResponseEntity<HomepageDTO>(homepageDTO, HttpStatus.OK);

    }
    @GetMapping("/getMore")
    public ResponseEntity<List<ShowInfor>> getMore(@RequestParam("page") int page){
        List<ShowInfor> homeRecommendList=  getHomeRecommend();
        List<ShowInfor> list = new ArrayList<>();
        for(int i=0;i<homeRecommendList.size();i++){
            if(i>=(page-1)* configInfo.getLimit()&&i<page*configInfo.getLimit()){
                list.add(homeRecommendList.get(i));
            }
        }
        return new ResponseEntity<List<ShowInfor>>(list,HttpStatus.OK);
    }



    //获取所有首页推荐的信息（资讯+商品）并按时间倒叙排序进行排序
    public List<ShowInfor> getHomeRecommend(){
        List<ShowInfor> list = new ArrayList<>();
        List<Information> informationList = informationRepository.getHomeRecommendInformation();
        for (Information information : informationList){
            ShowInfor homeRecommend = new ShowInfor();
            homeRecommend.setGuid(information.getInformationGuid());
            homeRecommend.setDescription(information.getDescription());
            homeRecommend.setImage(information.getImage());
            homeRecommend.setUpdateTime(information.getUpdateTime());
            homeRecommend.setType(InfoType.INFORMATION.getType());
            list.add(homeRecommend);
        }
        List<Commodity> commodityList = commodityRepository.getHomeRecommendCommodity();
        //homeRecommend.type:区分资讯/商品/活动
        for(Commodity commodity : commodityList){
            ShowInfor homeRecommend = new ShowInfor();
            homeRecommend.setGuid(commodity.getCommodityGuid());
            homeRecommend.setUpdateTime(commodity.getSellTime());
            homeRecommend.setImage(commodity.getImage());
            homeRecommend.setDescription(commodity.getDescription());
            homeRecommend.setPrice(commodity.getPrice());
            homeRecommend.setType(InfoType.COMMODITY.getType());
            list.add(homeRecommend);
        }
        Collections.sort(list, new Comparator(){
            @Override
            public int compare(Object o1, Object o2) {
                ShowInfor hr=(ShowInfor) o1;
                ShowInfor hr1=(ShowInfor) o2;
                if(hr.getUpdateTime().getTime()>hr1.getUpdateTime().getTime()){
                    return -1;
                }else if(hr.getUpdateTime().getTime()==hr1.getUpdateTime().getTime()){
                    return 0;
                }else{
                    return 1;
                }
            }
        });
        return list;
    }

}
